package com.hcnow.common.config

import com.hcnow.common.CommonHttpInterceptor
import com.hcnow.common.IntegrateHttpInterceptor
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter

@Configuration
class WebMVCConfig : WebMvcConfigurerAdapter() {
    /**
     * 添加拦截器
     */
    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(CommonHttpInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/api/auth/login");
        registry.addInterceptor(IntegrateHttpInterceptor()).addPathPatterns("/integrate/**");
    }

    /**
     * 添加跨域CORS配置
     */
    override fun addCorsMappings(registry: CorsRegistry?) {
        super.addCorsMappings(registry)
        registry?.addMapping("/api/**")
                ?.allowedHeaders("*")
                ?.allowedMethods("POST","GET","DELETE","PUT")
                ?.allowedOrigins("*");
    }

    /**
     * 添加资源文件路径映射
     */
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        super.addResourceHandlers(registry)
        registry?.addResourceHandler("swagger-ui.html")
                ?.addResourceLocations("classpath:/META-INF/resources/");
        registry?.addResourceHandler("/webjars/**")
                ?.addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}